package com.knife.bbs.action;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import com.knife.bbs.business.ActiveUser;
import com.knife.bbs.business.BBSDirService;
import com.knife.bbs.business.BBSRights;
import com.knife.bbs.business.BBSUserService;
import com.knife.bbs.business.ConfigService;
import com.knife.bbs.business.SkinService;
import com.knife.bbs.object.BBSDir;
import com.knife.bbs.object.Config;
import com.knife.bbs.object.Skin;
import com.knife.bbs.object.UserInfo;
import com.knife.web.ActionContext;
import com.knife.web.Module;
import com.knife.web.WebForm;
import com.knife.web.tools.AbstractCmdAction;

/**
 * 
 * <p>
 * Title:首页的页面数据
 * </p>
 * <p>
 * Description:从数据库中读取首页的相关数据!直接实现IWebAction接口即可.
 * </p>
 */
public abstract class BBSAction extends AbstractCmdAction {
	private ConfigService configService = ConfigService.getInstance();
	private SkinService skinService = SkinService.getInstance();
	private BBSDirService dirService = BBSDirService.getInstance();
	private BBSUserService userService = BBSUserService.getInstance();
	
	protected String contextPath = "";
	public Config config = null;
	public String skin = "default";

	public Object doBefore(WebForm form, Module module) {
		//获取全局配置
		config=configService.getConfig();
		if(config!=null){
			form.addResult("config", config);
		}
		//获取皮肤文件
		Skin myskin=skinService.getUsedSkin();
		if(myskin!=null){
			skin=myskin.getSkin();
		}
		//System.out.println("获取到的皮肤文件是："+skin);
		List<BBSDir> dirList = dirService.getRootDirs();
		form.addResult("dirList", dirList);
		try {
			contextPath = ActionContext.getContext().getRequest()
					.getContextPath();
		} catch (Exception e) {
		}
		HttpSession bbsSession = ActionContext.getContext().getSession();
		ServletContext Context = bbsSession.getServletContext();
		ServletContext Context1 = Context.getContext("/");	
		//正常论坛登录
		ActiveUser user = (ActiveUser) bbsSession.getAttribute("bbsuser");
		// 检查是否在会员系统登录
		String loginUser = "";
		try {
			HttpSession olSession = (HttpSession) Context1
					.getAttribute("loginuser");
			if (olSession != null) {
				if (olSession.getAttribute("loginuser") != null) {
					loginUser = olSession.getAttribute("loginuser").toString();
				}
			} else {
				//ActionContext.getContext().getSession().removeAttribute("bbsuser");
				//return module.findPage("/"+skin+"/", "login");
			}
			// System.out.println("论坛获取到的session值是:" + loginUser);
			if (loginUser.length() > 0) {
				UserInfo auser = userService.getUserInfoByName(loginUser);
				if (auser == null) {
					//System.out.println("执行激活跳转");
					form.addResult("username", loginUser);
					return module.findPage("/"+skin+"/", "active");
				} else {
					//System.out.println("执行登录跳转");
					user = userService.login(auser.getUsername(),
							auser.getPassword(), "0", ActionContext
									.getContext().getRequest().getRemoteAddr());
					bbsSession.setAttribute("bbsuser", user);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		}
		//System.out.println("-----获取到的用户名是:"+user);
		if (user != null)// 用户已经登录
		{
			UserInfo u = userService.getUserInfoByName(user.getUsername());
			if (u != null)
				form.addResult("user", u);
		}
		if (BBSRights.checkAdministrator(user)) {
			form.addResult("administrator", "1");
		}
		return super.doBefore(form, module);
	}
}
